﻿#include <iostream>
#include <graphics.h>
#include <string>
#include <vector>
#include<cmath>
#include"bullet.h"
#include"picture.h"
#include"button.h"
#include"player.h"
#include"enemy.h"
#include"bullet.h"
#include"jingying.h"
#include"jingying1.h"
using namespace std;
#pragma comment(lib,"MSIMG32.LIB")

void drawscore(int score)
{
	static TCHAR text[64];
	_stprintf_s(text, _T("现在玩家的分：%d"), score);
	setbkmode(TRANSPARENT);
	settextcolor(RGB(255, 85,185));
	outtextxy(10, 10, text);
}
void drawhealth(int health)
{
	static TCHAR text[64];
	_stprintf_s(text, _T("现在玩家的血量：%d"), health);
	setbkmode(TRANSPARENT);
	settextcolor(RGB(255, 85, 185));
	outtextxy(10, 30, text);
}


int main()
{
	initgraph(1280, 960,EW_SHOWCONSOLE);
	ExMessage msg;
	playerleft = new Atlas(_T("D:/Project1/Project1/back/player_left_%d.png"), 6);
	playerright = new Atlas(_T("D:/Project1/Project1/back/player_right_%d.png"), 6);
	enemyleft = new Atlas(_T("D:/Project1/Project1/back/enemy_left_%d.png"), 6);
	enemyright = new Atlas(_T("D:/Project1/Project1/back/enemy_right_%d.png"), 6);
	jingyingleft = new Atlas(_T("D:/Project1/Project1/back/jingying_left_%d.png"), 6);
	jingyingright = new Atlas(_T("D:/Project1/Project1/back/jingying_right_%d.png"), 6);
	jingying1left = new Atlas(_T("D:/Project1/Project1/back/jingying1_left_%d.png"), 6);
	jingying1right = new Atlas(_T("D:/Project1/Project1/back/jingying1_right_%d.png"), 6);
	Player player;
	IMAGE background;
	IMAGE memu;
	vector<Enemy*>enemys;
	vector<jingying*>jingyings;
	vector<jingying1*>jingying1s;
	RECT regionstart, regionfinish;
	regionstart.left = (1280 - buttonwidth) / 2;
	regionstart.right= (1280 + buttonwidth) / 2;
	regionstart.top = 430;
	regionstart.bottom = regionstart.top + buttonheight;
	regionfinish.left = (1280 - buttonwidth) / 2;
	regionfinish.right = (1280 + buttonwidth) / 2;
	regionfinish.top = 550;
	regionfinish. bottom= regionfinish.top + buttonheight;
	startbutton start_game = startbutton(regionstart, _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_idle.png"),
		_T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_hovered.png"), _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_start_pushed.png"));
	finishbutton finish_game = finishbutton(regionfinish, _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_idle.png"),
		_T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_hovered.png"), _T("D:/ConsoleApplication1/ConsoleApplication1/back/ui_quit_pushed.png"));
	loadimage(&memu, _T("D:/Project1/Project1/back/menu1.png"));
	loadimage(&background, _T("D:/Project1/Project1/back/background1.png"));
	int a=1;

	BeginBatchDraw();
	vector<Bullet*> playerBullets;
	int gongjitime = 10;
	int gongtime = 10;
	int baseBullets = 1;
	int ci = 1;
	int size = 5;
	int damage = 10;
	int qiang1 = 1;int qiang2 = 1;
	int qiang3 = 1;int qiang4 = 1;
	while (running)
	{
		DWORD time = GetTickCount();
		while (peekmessage(&msg))
		{
			
			if(gamestart==1)
			player.move1(msg);
			else
			{
				start_game.possessevent(msg);
				finish_game.possessevent(msg);
			}
		}
		if (gamestart)
		{
			gongtime++;
			if (score > 10&&qiang1==1)
			{
				int a = rand();
				if (a % 2 == 0)
					baseBullets++;
				else
					ci++;
				qiang1 = 0;
			}
			else if (score > 50 && qiang2 == 1)
			{
				int a = rand();
				if (a % 2 == 0)
					size =30;
				else
					playerhurt = 30;
				qiang2 = 0;
			}
			else if (score >70 && qiang3 == 1)
			{
				int a = rand();
				if (a % 2 == 0)
					baseBullets++;
				else
					ci++;
				qiang3 = 0;
			}
			else if (score > 100 && qiang4 == 1)
			{
				int a = rand();
				if (a % 2 == 0)
				baseBullets++;
				else
					int a = rand();
				if (a % 2 == 0)
					size = 50;
				else
					playerhurt = 30;
				qiang4 = 0;
			}
			// 在鼠标左键按下时的子弹生成逻辑中修改如下：
			if (gongtime >= gongjitime)
			{
				if (msg.message == WM_LBUTTONDOWN && gamestart)
				{
					POINT player_center = { player.getplayer().x + player_width / 2,
										  player.getplayer().y + player_height / 2 };
					POINT mouse_pos = { msg.x, msg.y };

					// 计算主方向向量
					double dir_x = mouse_pos.x - player_center.x;
					double dir_y = mouse_pos.y - player_center.y;
					double length = sqrt(dir_x * dir_x + dir_y * dir_y);
					if (length < 1e-5) { // 处理零向量
						dir_x = 0;
						dir_y = 1;
						length = 1;
					}
					double unit_dir_x = dir_x / length;
					double unit_dir_y = dir_y / length;

					// 计算垂直方向向量（逆时针90度）
					double perp_dir_x = -unit_dir_y;
					double perp_dir_y = unit_dir_x;

					// 行列间距参数
					const float col_spacing = 25.0f; // 列间距（水平方向）
					const float row_spacing = 30.0f; // 行间距（垂直方向）

					// 生成ci行baseBullets列的子弹阵列
					for (int row = 0; row < ci; ++row) {
						// 计算行偏移（沿主方向垂直方向）
						float row_offset = (row - (ci - 1) / 2.0f) * row_spacing;

						for (int col = 0; col < baseBullets; ++col) {
							// 计算列偏移（沿垂直方向）
							float col_offset = (col - (baseBullets - 1) / 2.0f) * col_spacing;

							// 计算子弹起始位置
							POINT bullet_start = {
								player_center.x + static_cast<int>(perp_dir_x * col_offset +
																  unit_dir_x * row_offset),
								player_center.y + static_cast<int>(perp_dir_y * col_offset +
																  unit_dir_y * row_offset)
							};

							// 子弹目标位置（保持原飞行方向）
							POINT bullet_target = {
								bullet_start.x + static_cast<int>(unit_dir_x * 1000),
								bullet_start.y + static_cast<int>(unit_dir_y * 1000)
							};

							playerBullets.push_back(new Bullet(bullet_start, bullet_target,size, 30, damage));
						}
					}
					gongtime = 0;
				}
			}
			for (auto& bullet : playerBullets) {
				bullet->update();
			}
			createnewjingying1(jingying1s, score);
			createnewjingying(jingyings,score);
			createnewenemy(enemys,score);
			player.move2();
			for (Enemy* enemy : enemys)
			{
				enemy->move(player);
			}
			for (jingying* enemy : jingyings)
			{
				enemy->move(player);
			}
			for (jingying1* enemy : jingying1s)
			{
				enemy->move(player);
			}
			/*bulletfollow(bullets, player);*/
			for (Enemy* enemy : enemys)
			{
				
				if (enemy->killplayer(player))
				{
					static TCHAR text[128];
					_stprintf_s(text, _T("目前玩家得分:%d"), score);
					MessageBox(GetHWnd(), text, _T("游戏结束"), MB_OK);
					running = 0;
					break;
				}
			}
			for (jingying1* enemy : jingying1s)
			{

				if (enemy->killplayer(player))
				{
					static TCHAR text[128];
					_stprintf_s(text, _T("目前玩家得分:%d"), score);
					MessageBox(GetHWnd(), text, _T("游戏结束"), MB_OK);
					running = 0;
					break;
				}
			}
			for (auto& jingying : jingyings) {
				jingying->updateBullets(); 
				for (auto& bullet : jingying->enemyBullets) {
					if (CheckCollision(player, bullet->position)) {
						playerhealth -= bullet->getdamage();
						bullet->active = false;
						if (jingying->killplayer(player)) {
							
								static TCHAR text[128];
								_stprintf_s(text, _T("目前玩家得分:%d"), score);
								MessageBox(GetHWnd(), text, _T("游戏结束"), MB_OK);
								running = 0;
								break;
							
						}
					}
				}
			}
			for (auto it = playerBullets.begin(); it != playerBullets.end();) {
				Bullet* bullet = *it;
				// 判断是否超出屏幕或失效
				if (!bullet->active ||
					bullet->position.x < 0 || bullet->position.x > 1280 ||
					bullet->position.y < 0 || bullet->position.y > 960) {
					delete bullet;
					it = playerBullets.erase(it);
				}
				else {
					++it;
				}
			}
			if (!enemys.empty())//如果enemys非空
			{
				for (auto it : enemys)//遍历
				{
					
					for (auto& bullet : playerBullets) {  // 遍历玩家子弹
							if (it->judge(*bullet)) {
								it->health -= playerhurt;
								it->zeroaida();
								it->hunt();
								bullet
									->active = false;  // 标记子弹失效
								a++;
							}
				     }
					
				}

			}
			if (!jingying1s.empty())//如果enemys非空
			{
				for (auto it : jingying1s)//遍历
				{

					for (auto& bullet : playerBullets) {  // 遍历玩家子弹
						if (it->judge(*bullet)) {
							it->health -= playerhurt;
							it->zeroaida();
							it->hunt();
							bullet->active = false;  // 标记子弹失效
							a++;
						}
					}

				}

			}
			if (!jingyings.empty())//如果enemys非空
			{
				for (auto it :jingyings)//遍历
				{
					
						for (auto& bullet : playerBullets) {  // 遍历玩家子弹
							if (it->judge(*bullet)) {
								it->health -= playerhurt;
								it->zeroaida();
								it->hunt();
								bullet->active = false;  // 标记子弹失效
								a++;
							}
						}
					
				}

			}
			
			for (size_t i = 0; i < enemys.size(); i++)
			{
				Enemy* enemy = enemys[i];
				if (!enemy->checklive())
				{
					swap(enemys[i], enemys.back());
					enemys.pop_back();
					delete enemy;
					score++;
				}
			}
			for (size_t i = 0; i < jingying1s.size(); i++)
			{
				jingying1* jingying1 = jingying1s[i];
				if (!jingying1->checklive())
				{
					swap(jingying1s[i], jingying1s.back());
					jingying1s.pop_back();
					delete jingying1;
					score=score+3;
				}
			}
			for (size_t i = 0; i < jingyings.size(); i++)
			{
				jingying* jingying= jingyings[i];
				if (!jingying->checklive())
				{
					swap(jingyings[i], jingyings.back());
					jingyings.pop_back();
					delete jingying;
					score=score+3;
				}
			}
		}
		
		cleardevice();
		
		if (gamestart)
		{
			putimage(0, 0, &background);
			drawscore(score);
			drawhealth(playerhealth);
			player.Drawplayer(1000 / 144);
			for (Enemy* enemy : enemys)
			{
				enemy->DrawEnemy(1000 / 144,player);
			}
			for (jingying1* enemy : jingying1s)
			{
				enemy->Drawjingying1(1000 / 144, player);
			}
			for (jingying* enemy : jingyings)
			{
				enemy->Drawjingying(1000 / 144, player);
			}
		     for (auto& bullet : playerBullets) {
				bullet->draw();
			}

		}
		else
		{
			putimage(0, 0, &memu);
			start_game.draw();
			finish_game.draw();
		}
		FlushBatchDraw();
		
		DWORD endtime = GetTickCount();
		DWORD time1 = endtime - time;
		if (time1 < 1000 / 144)
		{
			Sleep(1000 / 144 - time1);
		}
	}
	
	EndBatchDraw();
	for (auto& bullet : playerBullets) {
		delete bullet;
	}
	playerBullets.clear();
	delete playerleft;
	delete playerright;
	delete enemyleft;
	delete enemyright;
	return 0;
}